/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package com.json;

import java.util.ArrayList;  
import java.util.Iterator;  
import java.util.List;  

import net.sf.json.JSONArray;  

import com.google.gson.Gson;  
import com.google.gson.JsonArray;  
import com.google.gson.JsonElement;  
import com.google.gson.JsonObject;  
import com.google.gson.JsonParser;  

/** 
*@DEMO:napp 
*@Author:jilongliang 
*@Date:2013-7-20 
*/  
public class JsonParse {    
	
	static String json="{\"info\":{\"users\":{\"username\":\"liangjilong\",\"email\":\"jilongliang@sina.com\",\"tel\":\"12396878\",\"age\":\"25\"},\"address\":{\"street\":\"棠东\",\"province\":\"广东省\",\"district\":\"天河区\",\"city\":\"广州市\"}}}";
  private static Gson gson=new Gson();  
  /** 
   * 通过JsonLib把数组转换成Json数据 
   * @param url 
   * @throws Exception 
   */  
  private static String /*JSONArray*/ getArrayToJsonByJsonLib()throws Exception  
  {  
      //JSONArray jsonArr=null;  
      List<Address> lists=new ArrayList<Address>();  
      for (int i = 1; i <=12; i++) {  
          Address addr = new Address();   
          addr.setProvince("第"+i+"个省份");  
          addr.setCity("第"+i+"个城市");  
          addr.setCityCode("130"+i);  
          addr.setStreet("街道"+i);  
          addr.setStreetNumber("街道号码"+i);  
          lists.add(addr);  
      }  
      //jsonArr = JSONArray.fromObject(lists);  //把对象转换成Json对象  
      //return jsonArr;
      return gson.toJson(lists);
  }  
    
  public static void main(String[] args)throws Exception{  
      getJsonGson();   
        
  }  
    
  /** 
   * 利用Json-lib封装数组Json数据,Gson解析Json数据. 
   * @throws Exception 
   */  
  private static void getJsonGson() throws Exception {  
      //Json-lib的JSONArray数组对象  
      //JSONArray  JsonArray =getArrayToJsonByJsonLib();  
	  //String jsonData=JsonArray.toString();  
      
	  String jsonData=getArrayToJsonByJsonLib();
	  System.out.println(jsonData);
	  /* 
       * 自己组装成一个json格式，格式必须为{,,}才能为JsonElement对象的parse方法解析不是否则 
       * 会报错com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 1307 
       */  
      String json="{status:1,address:"+jsonData+"}";  
      System.out.println(json);
      // 创建一个JsonParser  
      JsonParser parser = new JsonParser();  
      JsonElement jsonEl = parser.parse(json);  
        
      JsonObject jsonObj = null;  
      jsonObj = jsonEl.getAsJsonObject();//转换成Json对象  
      String status=jsonObj.get("status").getAsString();//status节点  
      //System.out.println(status);  
        
      JsonElement addressEl=jsonObj.get("address");//address节点  
      if(addressEl.isJsonArray()){//判断是否为Json数组  
          JsonArray jsonArray= addressEl.getAsJsonArray();//转为数组  
          for(Iterator iter=jsonArray.iterator();iter.hasNext();){  
              JsonObject obj=(JsonObject) iter.next();  
              //String city=obj.get("city").getAsString();//方法一：根据key形势取值  
              Address add=gson.fromJson(obj, Address.class);//方法二：灵活使用Gson取数据  
              System.out.println(add.getProvince()+"\t"+add.getCity());  
           }  
      }  
  }  
     
}   